import { Component, OnInit } from '@angular/core';

/*
  使用angular内置的请求服务, 首先需要在 app.module.ts里先引入并注册 HttpClientModule 模块

  然后在当前组件里 引入HttpClient 服务, 在构造器里定义一下

  之后就可以在当前组件 使用 this.http.get()/ this.http.post() 等来请求数据
*/
// 引入服务模块
import { HttpClient } from '@angular/common/http'
// import { HttpClient, HttpHeaders } from '@angular/common/http'

@Component({
  selector: 'app-http',
  templateUrl: './http.component.html',
  styleUrls: ['./http.component.css']
})
export class HttpComponent implements OnInit {

  list: any[] = []

  constructor(public http: HttpClient) { }

  ngOnInit() {
  }

  getData() {
    let api = "http://rap2api.taobao.org/app/mock/304897/api/v1/full";
    this.http.get(api).subscribe((res: any) => {
      console.log(res);
      this.list = res.list
      
    })
  }

  /*
    post 请求时： 需要设置头部的话 要先引入 HttpHeaders
    const httpOptions = {
      headers: new HttpHeaders({'COntent-Type': 'application/json'})
    }

    this.http.post(api, {a: 1, b: 2}, httpOptions).subscribe((res: any) => {
      console.log(res);
    })
  */

}
